package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.dataobject.BaseDO;
import com.xiaoshuidi.cloud.module.rooms.mapper.ApartmentMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.Apartment;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

@Component
@RequiredArgsConstructor
public class ApartmentRepository extends ServiceImpl<ApartmentMapper, Apartment> {
    private final ApartmentMapper apartmentMapper;

    public IPage<Apartment> page(Page<Apartment> page, String name, Long apartmentId) {
        LambdaQueryWrapper<Apartment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(apartmentId), Apartment::getId, apartmentId);
        wrapper.like(ObjectUtil.isNotEmpty(name), Apartment::getName, name);
        return page(page, wrapper);
    }

    public Apartment myGetById(Long apartmentId) {
        Apartment apartment = this.getById(apartmentId);
        Assert.isTrue(ObjectUtil.isNotEmpty(apartment), StrUtil.format("门店信息不存在,id:{}", apartmentId));
        return apartment;
    }

    public List<Apartment> getByDeptId(Long deptId) {
        LambdaQueryWrapper<Apartment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Apartment::getDeptId, deptId);
        return this.list(wrapper);
    }

    public List<Apartment> getByName(String apartmentName) {
        LambdaQueryWrapper<Apartment> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Apartment::getName, apartmentName);
        return this.list(wrapper);
    }

    public List<Apartment> limit10List() {
        LambdaQueryWrapper<Apartment> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(BaseDO::getCreateTime)
                .isNotNull(Apartment::getIsConcentrated)
                .last("limit 10");
        return list(wrapper);
    }
}
